<?php

namespace plugin\admin\app\controller\image;


use plugin\admin\app\exception\AdminException;
use plugin\admin\app\logic\image\TestLogic;
use support\Request;
use support\Response;
use plugin\admin\app\controller\AuthController;
use support\exception\BusinessException;

class TestController extends AuthController
{
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        $contents = [];
        if(is_file(runtime_path('admin_image_test_param'))){
            $content = file_get_contents(runtime_path('admin_image_test_param'));
            $contents = json_decode($content, true);
        }

        $samplers = !empty($contents['samplers'])?$contents['samplers']:implode("\n",['DPM++ 3M SDE','DPM++ 2M SDE','DPM++ SDE','DPM++ 2M','UniPC','Restart']);

        $steps = !empty($contents['steps'])?$contents['steps']:implode("\n",[20,30,35,40,45]);

        $prompt = !empty($contents['prompt'])?$contents['prompt']:'Romantic scene between Xiaoyan (小雅) and Ajiang (阿杰), two lovers sitting on a bench in a quaint small town square, surrounded by cherry blossom trees, soft sunlight filtering through the leaves, their hands intertwined, smiling at each other with tender intimacy.';

        $aiImageCommand = !empty($contents['ai_image_command'])?$contents['ai_image_command']:config('param.ai_image_command_template','');

        return view('image/test/index',[
            'ai_image_command'=>$aiImageCommand,
             'samplers'=>$samplers,
             'steps'=>$steps,
             'prompt'=>$prompt,
             'modelids'=>json_encode($contents['modelids']??[],256),
        ]);
    }

    /**
     * 批量生成调式图
     * @param Request $request
     * @return Response
     * @throws AdminException
     */
    public function regenerateImage(Request $request): Response
    {
        return TestLogic::getInstance()->regenerateImage($request);
    }

    /**
     *  获取生成图像结果
     * @param Request $request
     * @return Response
     * @throws AdminException
     */
    public function getAiImageResult(Request $request): Response
    {
        return TestLogic::getInstance()->getAiImageResult($request);

    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return TestLogic::getInstance()->update($request);
        }
    }
}
